package com.example.dao.impl;

import com.example.dao.CollectionDao;
import com.example.model.Carousel;
import com.example.model.Collection;
import com.example.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CollectionDaoImpl implements CollectionDao {
    @Override
    public List<Collection> queryAll() {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Collection> list = new ArrayList<>();
        try {
            connection = DBUtil.getConnection();
            String sql = "select id,cname,price,content,cimg from tb_collection";
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                Collection collection = new Collection();
                collection.setId(rs.getInt("id"));
                collection.setCname(rs.getString("cname"));
                collection.setPrice(rs.getDouble("price"));
                collection.setContent(rs.getString("content"));
                collection.setCimg(rs.getString("cimg"));
                list.add(collection);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(connection,ps,rs);
        }
        return list;
    }
}
